﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CardLogistics.Model.CardMaking;
using System.Data.OracleClient;

namespace CardLogistics.DAL.CardMaking
{
    /// <summary>
    /// 卡片制作_白卡出库明细数据访问类
    /// </summary>
    public partial class CardMaking_CardOutStrgeDetailData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "CardMaking_CardOutStrgeDetail", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }


        /// <summary>
        /// 根据白卡出库单Id查询出库明细
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoByCheckId(string CheckId)
        {
            return GetList(" OUTSTORAGEID= '" + CheckId + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CardMaking_CardOutStrgeDetail GetModel(string rId)
        {
            CardMaking_CardOutStrgeDetail model = new CardMaking_CardOutStrgeDetail();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["OUTSTORAGEID"] != null)
                {
                    model.OUTSTORAGEID = row["OUTSTORAGEID"].ToString();
                }
                if (row["BOXNO"] != null)
                {
                    model.BOXNO = row["BOXNO"].ToString();
                }
                if (row["STARTNO"] != null)
                {
                    model.STARTNO = row["STARTNO"].ToString();
                }
                if (row["ENDNO"] != null)
                {
                    model.ENDNO = row["ENDNO"].ToString();
                }
                if (row["CARDNO"] != null)
                {
                    model.CARDNO = row["CARDNO"].ToString();
                }
                if (row["DUENUMBER"] != null && row["DUENUMBER"].ToString() != "")
                {
                    model.DUENUMBER = decimal.Parse(row["DUENUMBER"].ToString());
                }
                if (row["ACTUALNUMBER"] != null && row["ACTUALNUMBER"].ToString() != "")
                {
                    model.ACTUALNUMBER = decimal.Parse(row["ACTUALNUMBER"].ToString());
                }
                if (row["MISSINGCARDNO"] != null)
                {
                    model.MISSINGCARDNO = row["MISSINGCARDNO"].ToString();
                }
                
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(CardMaking_CardOutStrgeDetail model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
                    new OracleParameter("vOUTSTORAGEID", OracleType.NVarChar),
					new OracleParameter("vBOXNO", OracleType.VarChar,50),
					new OracleParameter("vSTARTNO", OracleType.VarChar,50),
					new OracleParameter("vENDNO", OracleType.VarChar,50),
					new OracleParameter("vCARDNO", OracleType.VarChar,50),
					new OracleParameter("vDUENUMBER", OracleType.Number,4),
					new OracleParameter("vACTUALNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.NVarChar)};

                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.OUTSTORAGEID;
                parameters[2].Value = model.BOXNO;
                parameters[3].Value = model.STARTNO;
                parameters[4].Value = model.ENDNO;
                parameters[5].Value = model.CARDNO;
                parameters[6].Value = model.DUENUMBER;
                parameters[7].Value = model.ACTUALNUMBER;
                parameters[8].Value = model.MISSINGCARDNO;
                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardOutStrgeDetail_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        /// <summary>
        /// 新增白卡出库明细
        /// </summary>
        /// <param name="ID">白卡出库单ID</param>
        /// <param name="FileValue">插入值</param>
        /// <returns></returns>
        public int AddDetail(string ID, string FileValue)
        {
            string[] split = FileValue.Split(';');//以分号分隔
            int count = split.Length; //计算行数
            int result = 0; //受影响的行数
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" delete from cardmaking_cardoutstrgedetail where OUTSTORAGEID ='" + ID + "'");
            result = CardLogistics.DAL.DbHelperSQL.ExecuteSql(strSql.ToString());
            for (int i = 0; i < count; i++)
            {
                strSql = new StringBuilder();//初始化
                strSql.Append("insert into cardmaking_cardoutstrgedetail");
                strSql.Append("(OUTSTORAGEID,BOXNO,CARDNO,STARTNO,ENDNO,DUENUMBER,ACTUALNUMBER,MISSINGCARDNO)");
                strSql.Append(" values(");
                strSql.Append("'" + ID + "',");
                strSql.Append("" + split[i] + "");
                strSql.Append(")");
                result = CardLogistics.DAL.DbHelperSQL.ExecuteSql(strSql.ToString());
            }
            return result;
        }

        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(CardMaking_CardOutStrgeDetail model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,50),
                    new OracleParameter("vOUTSTORAGEID", OracleType.NVarChar,50),
					new OracleParameter("vBOXNO", OracleType.VarChar,50),
					new OracleParameter("vSTARTNO", OracleType.VarChar,50),
					new OracleParameter("vENDNO", OracleType.VarChar,50),
					new OracleParameter("vCARDNO", OracleType.VarChar,50),
					new OracleParameter("vDUENUMBER", OracleType.Number,4),
					new OracleParameter("vACTUALNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.NVarChar),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.OUTSTORAGEID;
                parameters[2].Value = model.BOXNO;
                parameters[3].Value = model.STARTNO;
                parameters[4].Value = model.ENDNO;
                parameters[5].Value = model.CARDNO;
                parameters[6].Value = model.DUENUMBER;
                parameters[7].Value = model.ACTUALNUMBER;
                parameters[8].Value = model.MISSINGCARDNO;
                parameters[9].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardOutStrgeDetail_Update", parameters, parameters[9]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardOutStoDet_Del", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
